package com.luhuiguo.contacts.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public abstract class AsyncCallbackWithStatus<T> implements AsyncCallback<T> {

    private static LoadingIndicator loadingIndicator;

    public static final void setLoadingIndicator(LoadingIndicator indicator) {
        loadingIndicator = indicator;
    }

    public AsyncCallbackWithStatus() {
        setLoading(true);
    }

    @Override
    public final void onFailure(Throwable caught) {
        setLoading(false);
        onFailureInternal(caught);

    }

    @Override
    public final void onSuccess(T result) {
        setLoading(false);
        onSuccessInternal(result);
    }

    protected abstract void onSuccessInternal(T result);

    protected abstract void onFailureInternal(Throwable throwable);

    private void setLoading(boolean isLoading) {
        if (null != loadingIndicator) {
            if (isLoading) {
                loadingIndicator.show();
            } else {
                loadingIndicator.hide();
            }
        }
    }

}
